import { defineStore } from 'pinia'
import { ID_TOKEN } from '../constant'
import { localStorageMethods } from '../common/storage'

export const useUserStore = defineStore('userStore', {
  state: () => {
    return {
      token: '',
      name: '',
      userName: '',
      sex: '1',
      phoneNumber: '',
      customerNumber: '',
      email: '',
      roleList: [],
      hasLogin: false,
      showLogin: false,
      deptName: '',
      avatar: '',
      info: {} as any

    }
  },
  actions: {
    setIdToken(token: string) {
      return new Promise((resolve) => {
        this.token = token
        localStorageMethods.setLocalStorage(ID_TOKEN, token)
        resolve(token)
      })
    },
    setName(name: string) {
      this.name = name
    },
    setInfo(payload: any) {
      this.name = payload.name
      this.customerNumber = payload.customerNumber
      this.deptName = payload.deptName
      this.userName = payload.userName
      this.email = payload.email
      this.avatar = payload.avatar
      this.sex = payload.set
      this.phoneNumber = payload.phoneNumber
      this.roleList = payload.roleList || []
    }
  },
  getters: {
    getIdToken: (state) => {
      if (state.token) return state.token
      return localStorageMethods.getLocalStorage(ID_TOKEN)
    },
    getAllAttrs: (state) => state,
    getRole: (state) => state.roleList,
    getLoginState: (state): boolean => state.hasLogin,
    getCustomerNumber: (state): string => state.customerNumber
  }
})
